模块:Listen
本模塊嵌入 local mFileLink = require('Module:File link') local mTableTools = require('Module:TableTools') local mSideBox = require('Module:Side box') local p = {} function p.main(frame) local origArgs = frame:getParent().args local args = {} for k, v in pairs(origArgs) do v = v:match('^%s*(.-)%s*$') if v ~= '' then argsk = v end end return p._main(args) end function p._main(args) -- Organise the arguments by number. local numArgs = {} do local origNumArgs = mTableTools.numData(args) origNumArgs1 = origNumArgs.other -- Overwrite args.filename1 etc. with args.filename etc. origNumArgs = mTableTools.compressSparseArray(origNumArgs) for i, t in ipairs(origNumArgs) do if t.filename then numArgs+ 1 = t end end end -- Find whether we are outputting a plain or an embedded box. local isPlain = args.plain 'yes' local isEmbedded = args.embed and true or false -- Build the arguments for local sbargs = {} sbargs.metadata = 'no' sbargs.position = args.pos -- Style arguments do local style = {} if isPlain then style+ 1 = 'border:none' style+ 1 = 'background:transparent' style+ 1 = 'float:none' end if isEmbedded then style+ 1 = 'border-collapse:collapse' style+ 1 = 'border-width:1px 0 0 0' style+ 1 = 'background:transparent' style+ 1 = 'float:none' style+ 1 = 'margin:0 -5px' end if args.pos 'left' then style+ 1 = 'float:left' end style+ 1 = args.style sbargs.style = table.concat(style, '; ') end sbargs.textstyle = 'line-height:1.1em' -- Image if not isPlain and not isEmbedded then if args.image then sbargs.image = args.image else local images = { speech = 'Audio-input-microphone.svg', music = 'Gnome-mime-audio-openclipart.svg' } local image = args.type and imagesargs.type or 'Gnome-mime-sound-openclipart.svg' sbargs.image = mFileLink.new(image) :width(65) :height(50) :location('center') :link() :alt() :render() end end -- Text do local header if args.header then header = mw.html.create('div') header :css{ background = 'transparent', 'text-align' = 'left', padding = args.embed and '2px 0' or '2px' } :wikitext(args.header) header = tostring(header) header = header .. '\n' else header = '' end local text = {} for i, t in ipairs(numArgs) do text+ 1 = p.renderRow(t.filename, t.title, t.play, t.alt, t.description) end sbargs.text = header .. table.concat(text) end -- Below if not isPlain and not isEmbedded and args.help ~= 'no' then sbargs.below = string.format( '' .. "播放%s時有問題？請參閱媒體幫助。" .. , #numArgs > 1 and '這些-{zh-cn:文件; zh-tw:檔案;}-' or '此-{zh-cn:文件; zh-tw:檔案;}-' ) end -- Render the side box. local sideBox = mSideBox._main(sbargs) -- Render the tracking categories. local trackingCategories = p.renderTrackingCategories(args, numArgs) return sideBox .. trackingCategories end function p.renderRow(filename, title, play, alt, description) -- Renders the HTML for one file description row. if not filename then return nil end local root = mw.html.create() root :tag('div') :addClass('haudio') :newline() :tag('div') :css('padding', '2px 0') :wikitext(string.format('%s', filename, title or '')) :done() :newline() :tag('div') :css('padding-right', '4px') :wikitext( play ~= 'no' and mFileLink.new(filename):width(220):alt(alt):render() or nil ) :done() :newline() :tag('div') :css('padding', '2px 0 0 0') :addClass('description') :wikitext(description) :done() :done() :tag('hr', {selfClosing = true}) return tostring(root) end function p.renderTrackingCategories(args, numArgs, titleObj) -- Renders all tracking categories produced by the template. -- args and numArgs are passed through from p._main, -- and the titleObj is only used for testing purposes. local cats = {} local currentTitle = titleObj or mw.title.getCurrentTitle() if currentTitle.namespace 0 then -- We are in mainspace. cats+ 1 = '嵌入hAudio微格式的條目' for i, t in ipairs(numArgs) do local success, title = pcall(mw.title.new, 'Media:' .. t.filename) if success and title and not title.exists then cats+ 1 = '嵌入空的聽模板條目' break end end end if args.plain 'yes' then cats+ 1 = '使用plain參數的聽模板' end for i, cat in ipairs(cats) do catsi = string.format('Category:%s', cat) end return table.concat(cats) end return p